
#ifndef  KEY_C  
#define  KEY_C

#include "comm_rinnai.h"
#include "machine_stat.h"
#include "key_process.h"
#include "soft_time.h"
#include "sys_process.h"
#include "soft_time.h"
/*-----------------*
 |  Private Data
 *-----------------*/
#include "display.h"
#include "sc95f.h"
//#include "string.h"
#include "my_uart.h"
#include "sc95f_usci2.h"
#define ADD_KEY   3//?????
#define ELEMENT_NUMBER	 (6+ADD_KEY)//9
#define KEY_UP           0
#define KEY_PRESS_SHORT  1
#define KEY_PRESS_LONG   2
//用来判断在是否存在多个按键被按下，不包括组合键，
#define KEY_TIME_PRESS   20
#define KEY_PRESS_LONG_TIME           300//1.5S
#define KEY_PRESS_LONG_TIME_3S        600
#define KEY_PRESS_LONG_TIME_5S        1000





#define  KEY_counts          10
#define  KEY_up_conuts       5


static uint16_t Key_Map[ELEMENT_NUMBER]={
   // 0,
	KEY_TIMING,//0
	KEY_INCREASE,//1
	
	KEY_DECREASE,
	KEY_WIND_SPEED,
	KEY_STIR_FRY,
	KEY_SELF_HELP,

	KEY_INCREASE+KEY_TIMING,//WIFI
	KEY_DECREASE+KEY_WIND_SPEED,//TONGSUO
	KEY_DECREASE+KEY_INCREASE//VERSION
	
    
};
struct Key_Info 
{
    uint32_t key_mask;
    uint16_t  ke_stat[ELEMENT_NUMBER];
    uint16_t  key_count[ELEMENT_NUMBER];
    uint16_t  key_up_count[ELEMENT_NUMBER];
};

struct Key_Info key_info;

static KEY_NAMES key_Event_Id=KEY_NONE;
static uint8_t key_Same=0;
static KEY_NAMES key_Event_Id_Old=KEY_NONE;
uint32_t button_stat=0;


//uint8_t button1_count=0;
//uint8_t button2_count=0;
//uint8_t button3_count=0;
//uint8_t button4_count=0;
//uint8_t KEY_TIMING_SHORT_COUNT;
//uint8_t breath_or_all_show_flg;

//uint8_t flow_flg;
//uint8_t flow_flg_count;
//uint8_t key_cnt_test=0;


 /*---------------------------------------------------------------------------*
 |  KeyInit
 |
 |  Initialize the key module. Called once on power-up.
 |
 |  For details see Key.h
 *---------------------------------------------------------------------------*/
void KeyInit(void)
{
	
  	uint8_t i=0;

	        for(i=0;i<(ELEMENT_NUMBER);i++)  {
            key_info.ke_stat[i]=0;
						key_info.key_count[i]=0;
						key_info.key_up_count[i]=5;
        }
	
    button_stat=0;
}

void Event_Clean(void)
{
    uint8_t i;
    
        key_Event_Id=KEY_NONE;
        key_Event_Id_Old=KEY_NONE;
        key_Same=0;
        for(i=0;i<(ELEMENT_NUMBER);i++)  {
            key_info.ke_stat[i]=KEY_UP;
        }
}
extern unsigned long exKeyValueFlag ;
/*---------------------------------------------------------------------------*
 |  KeyEvent
 |
 |  For details see Key.h
 *---------------------------------------------------------------------------*/


uint8_t KeyEvent(void)
{
    uint8_t event_id=0;

    uint8_t i;
    for(i=0;i<(ELEMENT_NUMBER);i++){
        //只有长按和短按进行判断，其他情况不进行处理
    	if(key_info.ke_stat[i]==KEY_PRESS_SHORT||key_info.ke_stat[i]==KEY_PRESS_LONG) 
			event_id= 2*i+ key_info.ke_stat[i];//key_info.ke_stat ???? 0,1,2
    }
    return event_id; 
}

/*---------------------------------------------------------------------------*
 |  KeyKeyProcess
 |
 |  Called once per line sync.  For details see Key.h
 *---------------------------------------------------------------------------*/
void KeyProcess(void)
{
    uint8_t i;
    uint8_t j;
		button_stat=exKeyValueFlag;
    //static uint32_t key_mask_old=0;
	//Button_stat();//????
	// ???		??1		??2			??3	     ??1+??3		??4			??5				??2+??3||(button_stat==0x06)
	if((button_stat==0)||(button_stat==1)||(button_stat==2)||(button_stat==0x04)||(button_stat==0x08)||(button_stat==0x10)||(button_stat==0x20)||(button_stat==0x40)||(button_stat==0x80)){
				key_info.key_mask = button_stat;//
	}
    else
    {
        //组合键情况，当前按下，但是并不是设定组合键，而且组合键其中一个按键已经进行了长按，
        for(i=0;i<(ELEMENT_NUMBER);i++)
        {
            //找出最先按下的按键，最先按下的，最先到达长按时间
            if(((button_stat&Key_Map[i])!=0)&&(key_info.key_count[i]==0x4b))
            {
                //再次遍历，找出后面按下的，为了防止后面按下了两三个，所以在这里不进行跳出，
                for(j=0;j<(ELEMENT_NUMBER);j++)
                {
                    if((i!=j)&&((button_stat&Key_Map[j])!=0))
                    {
                        key_info.key_count[j]=0; 
                        //把它设置成这个标记位
                        key_info.ke_stat[j]=KEY_TIME_PRESS;
                    }
                }
            }
        }
        
    }

    for(i=0;i<ELEMENT_NUMBER;i++)//20mS
	{
    	if(key_info.key_mask == Key_Map[i]){ 
            //存在短暂的拉高
            if(key_info.key_up_count[i]<KEY_up_conuts&&key_info.key_up_count[i]>0)
            {
                //出现一次很短的按下，然后抬起，抬起时间也很短
                //按下次数小于八次，小于40MS，马上又抬起了
                if(key_info.key_count[i]<KEY_counts)
                {
                    key_info.key_count[i]=0;
                }
                key_info.key_up_count[i]=0;
            }
            //当前第一次进来，上拉计数清空。
            if(key_info.key_count[i]==0)
            {
                key_info.key_up_count[i]=0;
            }
            if(key_info.ke_stat[i]!=KEY_TIME_PRESS)
            {
            	key_info.key_count[i]++;
            }
            if(key_info.key_count[i]>=KEY_PRESS_LONG_TIME)
            {
            	key_info.key_count[i]=KEY_PRESS_LONG_TIME;
            }
//            else if((i>=3&&i<ELEMENT_NUMBER)&&key_info.key_count[i]>=KEY_PRESS_LONG_TIME_3S)
//			{
//				key_info.key_count[i]=KEY_PRESS_LONG_TIME_3S;
//			}
//            else if((i==0)&&key_info.key_count[i]>=KEY_PRESS_LONG_TIME_5S)
//			{
//				key_info.key_count[i]=KEY_PRESS_LONG_TIME_5S;
//			}
        }
        else{
					
					if(i==1)
					{
							i=3;
							i=1;
					}	
					//????
				//两种情况，1是，组合键，清掉分键的计数，2是，后按的按键标记数被清理掉
				if((key_info.key_mask&Key_Map[i])!=0)//?????,??????
				{
					key_info.key_count[i]=0;
					key_info.ke_stat[i]=KEY_UP;
				}
				//当前这个按键处于上拉状态
				key_info.key_up_count[i]++;
				//最大五次
				if(key_info.key_up_count[i]>=KEY_up_conuts)
				{
					key_info.key_up_count[i]=KEY_up_conuts;
					//假设存在短暂拉低。
					if(key_info.key_count[i]<KEY_counts)
					{
						key_info.key_count[i]=0;
					}

				}
				if(key_info.key_up_count[i]==KEY_up_conuts&&(key_info.ke_stat[i]>0))//||key_info.ke_stat[i]==0
				{
					key_info.ke_stat[i]=KEY_UP;
					key_info.key_count[i]=0;
					if(button_long_flg)
					{
						button_long_flg=0;
					}
					
				}

           }
                              
    }

}
void button_process(void)
{
	int i;
	for(i=0;i<ELEMENT_NUMBER;i++)
	{
//		if(i!=0&&3!=i&&i!=4)
//		{
//			if(key_info.key_count[i]>=KEY_counts&&key_info.key_count[i]<KEY_PRESS_LONG_TIME)
//			{
//				key_info.ke_stat[i]=KEY_PRESS_SHORT;
//			}
//			else if((i==1||i==2)&&key_info.key_count[i]>=KEY_PRESS_LONG_TIME)
//			{
//				key_info.ke_stat[i]=KEY_PRESS_LONG;
//				//key_info.key_count[i]=0;
//			}
//			else if((i>4&&i<ELEMENT_NUMBER)&&key_info.key_count[i]>=KEY_PRESS_LONG_TIME_3S)
//			{
//				key_info.ke_stat[i]=KEY_PRESS_LONG;
//			}
//			//key_info.key_count[i]=0;
//		}
//		else
			if(i==0||3==i||i==4||i==5||i==1||i==2)
			{
			//按下次数大于三次，大于40MS，马上又抬起了
			//key_info.key_up_count[i]++;
			 //判断是不是长按
			if(key_info.key_count[i]>=KEY_PRESS_LONG_TIME)//&&key_mask_old==KEY_count
			{
				key_info.ke_stat[i]=KEY_PRESS_LONG; //press long  *50ms
				
			

				//key_info.key_count[i]=0;
			}
			if((i==0)&&key_info.key_count[i]>=KEY_PRESS_LONG_TIME)//&&key_mask_old==KEY_count
			{
				key_info.ke_stat[i]=KEY_PRESS_LONG; //press long  *50ms
				//key_info.key_count[i]=0;
			}
			else if((i==0)&&(key_info.key_count[i]>=KEY_counts)&&(key_info.key_count[i]<KEY_PRESS_LONG_TIME)&&(key_info.key_up_count[i]>=KEY_up_conuts))//开关机按键按下时间小于5S时
			{
				key_info.ke_stat[i]=KEY_PRESS_SHORT;
			}
			//判断是不是短按
			else if((key_info.key_count[i]>=KEY_counts&&key_info.key_count[i]<KEY_PRESS_LONG_TIME) &&key_info.key_up_count[i]>=KEY_up_conuts)//&&key_mask_old==KEY_count
			{
				key_info.ke_stat[i]=KEY_PRESS_SHORT; //press short 5*50ms
				//key_info.key_count[i]=0;
			}
			//按下计时清零
			//key_info.key_count[i]=0;
		}
	}
	//进行赋值
    key_Event_Id=KeyEvent();
    if( key_Event_Id_Old==key_Event_Id) key_Same=1;
    else key_Same=0;
    key_Event_Id_Old=key_Event_Id;

}
/*---------------------------------------------------------------------------*
 |  KeyBufferClear
 |
 |  Clear buffer of all key presses.  For details see Key.H
 *---------------------------------------------------------------------------*/
uint8_t EventListener( KEY_NAMES  keyEvent)
{
//    if(key_Event_Id_Old==key_Event_Id)  return 0;
    
    if(keyEvent==KEY_NONE && (key_Event_Id==KEY_NONE || key_Same==1) )  {
        
//        if( key_Same==1)  DBUG_PutString(" key_Same \r\n");
        return 1;   
    }
    
    if(keyEvent==key_Event_Id && key_Same==0) {
        return 1;
    }
//    if(keyEvent==key_Event_Id && key_Event_Id==KEY_INCREASE_LONG) {

//        return 1;
//    }
//    if(keyEvent==key_Event_Id && key_Event_Id==KEY_POWER_LONG) {

//           return 1;
//    }
//    if(keyEvent==key_Event_Id && key_Event_Id==KEY_DECREASE_LONG) {
//        return 1;
//    }
    return 0;
}









#endif



